﻿using System;
using System.Windows.Controls;
using System.Data;
using System.Collections.Generic;
using GrainManage.Tools;

namespace GrainManage.Controls
{

    /// <summary>
    /// Interaction logic for Pager.xaml
    /// </summary>
    public partial class Pager : UserControl
    {
        public Pager()
        {
            InitializeComponent();

            BindControlEvent();
        }

        private readonly Dictionary<string, Action<int, int>> serviceList = new Dictionary<string, Action<int, int>>();

        private readonly PageHelper help = new PageHelper();

        private string currentService = null;

        private void BindControlEvent()
        {
            btnNavigate.Click += (sender, e) => Navigate();
            btnFirst.Click += (sender, e) => NavigateTo(0);
            btnLast.Click += (sender, e) => NavigateTo(help.MaxPageIndex);
            btnPrevious.Click += (sender, e) => NavigateTo(help.PreviousPageIndex);
            btnNext.Click += (sender, e) => NavigateTo(help.NextPageIndex);

            txtCurrent.MouseDoubleClick += (sender, e) => Navigate();
            txtCurrent.KeyUp += (sender, e) => { if (e.Key == System.Windows.Input.Key.Enter) { Navigate(); } };
        }

        private void Navigate()
        {
            int current = 0;
            if (int.TryParse(txtCurrent.Text, out current))
            {
                NavigateTo(current - 1);
            }
        }

        private void NavigateTo(int pageIndex)
        {
            if (!string.IsNullOrEmpty(currentService) && serviceList.ContainsKey(currentService))
            {
                var service = serviceList[currentService];
                if (service != null)
                {
                    help.CurrentPageIndex = pageIndex;
                    service(help.CurrentPageIndex, help.PageSize);
                }
            }
        }

        public void RegisterService(Action<int, int> service, string serviceName)
        {
            serviceList[serviceName] = service;
        }

        public void StartService(string serviceName)
        {
            if (currentService != serviceName)
            {
                currentService = serviceName;
            }
            NavigateTo(0);
        }

        public void Refresh()
        {
            txtCurrent.Text = help.CurrentPageNumber.ToString();
            tbTotalPage.Text =help.MaxPageNumber.ToString();
            tbTotalRecord.Text = help.TotalRowNumber.ToString();
        }

        //public void Refresh(DataSet data)
        //{
        //    if (data != null && data.Tables != null)
        //    {
        //        help.TotalRowNumber = data.Tables.Count > 1 ? Convert.ToInt32(data.Tables[1].Rows[0][0]) : data.Tables[0].Rows.Count;
        //        Refresh();
        //    }
        //}

        public int PageSize
        {
            get { return help.PageSize; }
            set { help.PageSize = value; }
        }

        public int TotalRowCount
        {
            get { return help.TotalRowNumber; }
            set { help.TotalRowNumber = value; Refresh(); }
        }
    }
}
